/**
 * ESP32-Arduino-Framework
 * Arduino开发环境下适用于ESP32芯片系列开发板的应用开发框架。
 * 
 * Author: Billy Zhang（billy_zh@126.com）
 */
#include "sleep_timer.h"
#include "src/app/application.h"
#include "src/boards/board.h"
#include "src/display/display.h"
#include "src/sys/log.h"

#include <esp_sleep.h>
#include <esp_pm.h>

#if CONFIG_USE_LVGL==1
#include "src/libs/esp_lvgl_port/esp_lvgl_port.h"
#endif

#define TAG "SleepTimer"

SleepTimer::SleepTimer(int seconds_to_light_sleep, int seconds_to_deep_sleep)
    : seconds_to_light_sleep_(seconds_to_light_sleep), seconds_to_deep_sleep_(seconds_to_deep_sleep) {
    sleep_ticker_ = new Ticker();
}

SleepTimer::~SleepTimer() {
    if( sleep_ticker_ != nullptr) {
        sleep_ticker_->detach();
        sleep_ticker_=nullptr;
    }
}

void TickerCallback(SleepTimer *arg) {
    arg->CheckSleep();
}

void SleepTimer::SetEnabled(bool enabled) {
    if (enabled && !enabled_) {
        ticks_ = 0;
        enabled_ = enabled;
        sleep_ticker_->attach(1, TickerCallback, this);
        Log::Info( TAG, "Timer started" );
    } else if (!enabled && enabled_) {
        sleep_ticker_->detach();
        Log::Info( TAG, "Timer stopped" );
        enabled_ = enabled;
        WakeUp();
    }
}

void SleepTimer::OnEnterLightSleepMode(std::function<void()> callback) {
    on_enter_light_sleep_mode_ = callback;
}

void SleepTimer::OnExitLightSleepMode(std::function<void()> callback) {
    on_exit_light_sleep_mode_ = callback;
}

void SleepTimer::OnEnterDeepSleepMode(std::function<void()> callback) {
    on_enter_deep_sleep_mode_ = callback;
}

void SleepTimer::CheckSleep() {
    auto& app = Application::GetInstance();
    if (!app.CanEnterSleepMode()) {
        ticks_ = 0;
        return;
    }

    ticks_++;
    if (seconds_to_light_sleep_ != -1 && ticks_ >= seconds_to_light_sleep_) {
        if (!in_light_sleep_mode_) {
            in_light_sleep_mode_ = true;
            if (on_enter_light_sleep_mode_) {
                on_enter_light_sleep_mode_();
            }
        
            app.Schedule([this]() {
                while (in_light_sleep_mode_) {
                    auto& board = Board::GetInstance();
                    board.GetDisplay()->UpdateStatusBar(true);

#if CONFIG_USE_LVGL==1
                    lv_refr_now(nullptr);
                    lvgl_port_stop();
    
                    // 配置timer唤醒源（30秒后自动唤醒）
                    esp_sleep_enable_timer_wakeup(30 * 1000000);
                    
                    // 进入light sleep模式
                    esp_light_sleep_start();
                    lvgl_port_resume();
#endif
                    auto wakeup_reason = esp_sleep_get_wakeup_cause();
                    if (wakeup_reason != ESP_SLEEP_WAKEUP_TIMER) {
                        break;
                    }
                }
                WakeUp();
            });
        }
    }
    if (seconds_to_deep_sleep_ != -1 && ticks_ >= seconds_to_deep_sleep_) {
        if (on_enter_deep_sleep_mode_) {
            on_enter_deep_sleep_mode_();
        }

        esp_deep_sleep_start();
    }
}

void SleepTimer::WakeUp() {
    ticks_ = 0;
    if (in_light_sleep_mode_) {
        in_light_sleep_mode_ = false;
        if (on_exit_light_sleep_mode_) {
            on_exit_light_sleep_mode_();
        }
    }
}
